
#include "..\UnitTest\ut_features.h"
#include "..\UnitTest\ut_runner.h"

#include "cx_file.h"

/*************************************************************************
* UT CASE
*************************************************************************/

const QTCHAR* pNoFile = L"c:\\nofile.txt";
const QTCHAR* pRDFile = L"c:\\testrd.txt";

UT_CASE( __test_enter )
{
	ut_trace( "[  AUTHOR  ] xiaotianchen" );

	return UT_RESULT_PASS;
}

UT_CASE( __test_open )
{
	CXFILE handle = { 0 };
	
	// nofile on disk
	handle = cx_file_open(pNoFile, modeRead);
	UT_EXPECT_OR_RETURN( cx_file_isValid(handle) == QFALSE, UT_RESULT_FAIL );
	
	handle = cx_file_open(pNoFile, modeWrite);
	UT_EXPECT_OR_RETURN( cx_file_isValid(handle) == QFALSE, UT_RESULT_FAIL );
	
	handle = cx_file_open(pNoFile, modeReadWrite);
	UT_EXPECT_OR_RETURN( cx_file_isValid(handle) == QFALSE, UT_RESULT_FAIL );

	
	// create one file
	handle = cx_file_open(pRDFile, modeCreate);
	UT_EXPECT_OR_RETURN( cx_file_isValid(handle) == QTRUE, UT_RESULT_FAIL );
	
	if (cx_file_isValid(handle))
	{
		cx_file_close(&handle);
	}

	return UT_RESULT_PASS;
}

UT_CASE( __test_readwrite )
{
	CXFILE	handle = { 0 };
	char	pReadBuf[20] = {0};
	
	const char* pWriteBuf = "test234342";
	
	handle = cx_file_open(pRDFile, modeWrite);
	UT_EXPECT_OR_RETURN( cx_file_isValid(handle) == QTRUE, UT_RESULT_FAIL );
	
	cx_file_write(handle, pWriteBuf, strlen(pWriteBuf));
	
	cx_file_close(&handle);
	
	handle = cx_file_open(pRDFile, modeRead);
	UT_EXPECT_OR_RETURN( cx_file_isValid(handle) == QTRUE, UT_RESULT_FAIL );
	
	cx_file_read(handle, pReadBuf, strlen(pWriteBuf));
	pReadBuf[strlen(pWriteBuf)] = '\0';
	
	cx_file_close(&handle);
	
	UT_EXPECT_OR_RETURN( strcmp(pWriteBuf, pReadBuf) == 0, UT_RESULT_FAIL );
	
	return UT_RESULT_PASS;
}

UT_CASE( __test_getsize )
{
	CXFILE		handle = { 0 };
	const int	KLen = 10;
	
	handle = cx_file_open(pRDFile, modeRead);
	UT_EXPECT_OR_RETURN( cx_file_isValid(handle) == QTRUE, UT_RESULT_FAIL );
	
	UT_EXPECT_OR_RETURN( cx_file_size(handle) == KLen, UT_RESULT_FAIL );
	
	cx_file_close(&handle);
	
	return UT_RESULT_PASS;
}

UT_CASE( __test_leave )
{
	return UT_RESULT_PASS;
}

/*************************************************************************
* UT GROUP
*************************************************************************/

UT_GROUP( AcceptanceTest_CX_FILE )
{
	UT_ADD_CASE( __test_enter ),
			
	UT_ADD_CASE( __test_open ),
	UT_ADD_CASE( __test_readwrite ),
	UT_ADD_CASE( __test_getsize ),

	UT_ADD_CASE( __test_leave ),

	UT_LAST_CASE
};
